package edu.gj.demo;

import com.google.gson.Gson;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Simple
 * @date 2020/4/16 13:48
 */
public class Server {
    private static List<ServerThread> serverThreads = new ArrayList<ServerThread>();

    public static void main(String[] args) throws IOException {

        int count = 0;

        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("Server is start");
        while (true) {
            count++;
            Socket socket = serverSocket.accept();
            System.out.println("accept client count:"+count);
            ServerThread serverThread = new ServerThread(socket);
            serverThreads.add(serverThread);
            serverThread.start();
        }
    }


    public static void sendMessage(MessageBean messageBean){
        for (ServerThread serverThread :
                serverThreads) {
            if (serverThread.getClientName().equals(messageBean.getTo())){
                serverThread.sendMessage(new Gson().toJson(messageBean));
            }
        }
    }


}
